package com.feng.financial.common.config;

import com.feng.financial.common.aspect.RequestMappingAspect;
import com.feng.financial.common.exception.WebErrorController;
import com.feng.financial.common.exception.WebExceptionHandle;
import com.feng.financial.common.properties.CommonProperties;
import com.feng.financial.common.spring.ApplicationContextHolder;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.stream.Collectors;

/**
 * @author Administrator
 */
@EnableConfigurationProperties(CommonProperties.class)
@Configuration
public class CommonConfig {

    @Bean
    public ApplicationContextHolder applicationContextHolder(){
        return new ApplicationContextHolder();
    }

    @Bean
    public RequestMappingAspect requestMappingAspect(){
        return new RequestMappingAspect();
    }

    @Bean
    public WebExceptionHandle webExceptionHandle() {
        return new WebExceptionHandle();
    }

    @Bean
    public WebErrorController basicErrorController(ErrorAttributes errorAttributes,
                                                     ObjectProvider<ErrorViewResolver> errorViewResolvers,
                                                     ServerProperties serverProperties) {
        return new WebErrorController(errorAttributes, serverProperties.getError(),
                errorViewResolvers.orderedStream().collect(Collectors.toList()));
    }

}
